package com.lindroy.androidplayer.viewmodels

import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.filter
import androidx.paging.map
import com.lindroy.androidplayer.utils.mapSingle
import com.lindroy.androidplayer.logic.model.bean.TabBean
import com.lindroy.androidplayer.logic.network.repository.Repository
import com.lindroy.androidplayer.utils.SingleLiveEvent
import kotlinx.coroutines.flow.map

/**
 * @author Lin
 * @date 2022/1/24
 * @function
 */
class KnowledgeViewModel : PageViewModel() {

    private val _itemClickAction = SingleLiveEvent<List<TabBean>>()

    private val _childItemClickAction = SingleLiveEvent<List<TabBean>>()

    val itemClickAction = _itemClickAction.mapSingle { it }

    val childItemClickAction = _childItemClickAction.mapSingle { it }

    val knowledgeSystemList = Repository.knowledgeSystemList.map { pageData ->
        pageData.filter { it.knowledgeItems.isNotEmpty() }.map {
            KnowledgeSystemViewModel(
                id = it.id,
                name = it.name,
                items = it.knowledgeItems,
                itemClickAction = _itemClickAction,
                childItemClickAction = _childItemClickAction
            )
        }
    }.cachedIn(viewModelScope)

}